<!DOCTYPE html>
<html>
<head>
    <title>{{if .post}}编辑文章{{else}}写文章{{end}}</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <!-- 引入 Markdown 编辑器 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/easymde/dist/easymde.min.css">
    <script src="https://cdn.jsdelivr.net/npm/easymde/dist/easymde.min.js"></script>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <h1>{{if .post}}编辑文章{{else}}写文章{{end}}</h1>
                <form method="post" action="{{if .post}}/admin/post/edit/{{.post.ID}}{{else}}/admin/post/new{{end}}" class="mt-4">
                    <div class="mb-3">
                        <label for="title" class="form-label">标题</label>
                        <input type="text" class="form-control" id="title" name="title" value="{{if .post}}{{.post.Title}}{{end}}" required>
                    </div>
                    <div class="mb-3">
                        <label for="content" class="form-label">内容</label>
                        <textarea class="form-control" id="content" name="content" rows="10">{{if .post}}{{.post.Text}}{{end}}</textarea>
                    </div>
                    <div class="mb-3">
                        <label for="categories" class="form-label">分类</label>
                        <select class="form-select" id="categories" name="categories" multiple>
                            {{range .categories}}
                            <option value="{{.ID}}" {{if $.post}}{{range $.post.Categories}}{{if eq .ID $.ID}}selected{{end}}{{end}}{{end}}>{{.Name}}</option>
                            {{end}}
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="status" class="form-label">状态</label>
                        <select class="form-select" id="status" name="status">
                            <option value="publish" {{if and .post (eq .post.Status "publish")}}selected{{end}}>发布</option>
                            <option value="draft" {{if and .post (eq .post.Status "draft")}}selected{{end}}>草稿</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="slug" class="form-label">别名</label>
                        <input type="text" class="form-control" id="slug" name="slug" value="{{if .post}}{{.post.Slug}}{{end}}">
                        <div class="form-text">用于生成文章的永久链接，建议使用字母、数字和连字符</div>
                    </div>
                    <button type="submit" class="btn btn-primary">保存</button>
                    <a href="/admin/posts" class="btn btn-secondary">返回</a>
                </form>
            </div>
        </div>
    </div>
    <script>
        // 初始化 Markdown 编辑器
        var easyMDE = new EasyMDE({
            element: document.getElementById('content'),
            spellChecker: false,
            status: false,
            autosave: {
                enabled: true,
                uniqueId: "post_content"
            }
        });
    </script>
</body>
</html>